package com.webside.points.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.webside.points.model.IssueEntity;
import com.webside.points.model.IssueTypeEntity;
import com.webside.points.model.TopSearchEntity;
import com.webside.points.model.UserEntity;
import com.webside.points.service.AdvertisingService;
import com.webside.points.service.IssueService;
import com.webside.points.service.IssueTypeService;
import com.webside.points.service.TopSearchService;
import com.webside.points.service.UserService;
import com.webside.points.wechat.PageUtil;

@Controller
@Scope("prototype")
@RequestMapping("/user/")
public class UserController {

	@Autowired
	UserService userService;
	
	@Autowired
	IssueService issueService;
	
	@Autowired
	IssueTypeService issueTypeService;
	
	@Autowired
	AdvertisingService advertisingService;
	
	@Autowired
	TopSearchService topSearchService;
	
	
	@RequestMapping("findAllUI")
	public String findAllUI(Model model){
		
		PageUtil pageUtil=new PageUtil();
		UserEntity userEntity=new UserEntity();
		int totalCount=userService.findCount(userEntity);
		pageUtil.setTotalCount(totalCount);
		pageUtil.setCurPage(1);
		model.addAttribute("pageUtil", pageUtil);
		return "serve/user";
	}
	
	@RequestMapping("findAll")
	public void findAll(Model model,UserEntity userEntity,HttpServletResponse response){
		System.out.println("进入查询用户：>>>"+userEntity);
		 PageUtil pageUtil=new PageUtil();
		 
		 if(String.valueOf(userEntity.getCurPage())!=null){
			 int cPage=Integer.valueOf(userEntity.getCurPage());
			 cPage=(cPage-1)*10;
			 userEntity.setCurPage(Integer.valueOf(cPage));
		 }
		 
		 List<UserEntity> list=userService.findAll(userEntity);
		  pageUtil.setContent(list);
		  model.addAttribute("pageUtil", pageUtil);
		  
		  String json = JSON.toJSONString(pageUtil);
			try {
				response.getWriter().write(json);
				response.flushBuffer();
			} catch (Exception e) {
				System.out.println("JsonError："+e);
			}
	}
	
	@RequestMapping("insert")
	public String insert(HttpServletRequest request){
		UserEntity userEntity=new UserEntity();
		Map<?,?> map=(Map<?,?>)request.getSession().getAttribute("map");
		
		System.out.println("map:>>>"+map);
		
		if(userService.findByUnionId((String) map.get("unionid"))==null){
		//if(userService.findByUnionId("abc")==null){
			System.out.println("进入用户信息增加>>>");
			userEntity.setOpenId((String) map.get("openid"));
			userEntity.setWechatId((String) map.get("nickname"));
			userEntity.setProvince((String) map.get("province"));
			userEntity.setHeadUrl((String) map.get("headimgurl"));
			userEntity.setUnionId((String) map.get("unionid"));
			userEntity.setStatus("1");
			userEntity.setManageStatus("2");
			userEntity.setStudyNumber(0);
			//userEntity.setUnionId("abc");
			userEntity.setCreateTime(new Date(System.currentTimeMillis()));
			userEntity.setPayment(0.00);
			userService.insert(userEntity);
		}else{
			if(userService.findByOpenId((String) map.get("openid"))==null){
			//if(userService.findByOpenId("ozHQjvzQWkiv85kwOoZhqyQGcnVA")==null){
				System.out.println("修改openId"+(String) map.get("openid"));
				userEntity.setOpenId((String) map.get("openid"));
				userEntity.setUnionId((String) map.get("unionid"));
				//userEntity.setUnionId("abc");
				userService.updateOpenId(userEntity);
			}
		}
		 
		String url="redirect:../issueType/findAll?mark=service&openId="+(String) map.get("openid");
		
		return url;
	}
	
	@RequestMapping("findByOpenId")
	public String findByOpenId(String openId,Model model){
		UserEntity user =userService.findByOpenId(openId);
		
		if(user!=null){
			System.out.println(openId);
			
			System.out.println(user);
			
			model.addAttribute("user", user);
			/*
			 * 提问数量
			 */
			Map<String,Integer> issue=new HashMap<String,Integer>();
			issue.put("userId", user.getId());
			List<IssueEntity> issueList=issueService.findRecord(issue);
			model.addAttribute("issueCount", issueList.size());
			/*
			 * 回答数量
			 */
			if(user.getExpertId()!=0){
				Map<String,Integer> expert=new HashMap<String,Integer>();
				expert.put("expertId", user.getExpertId());
				List<IssueEntity> expertList=issueService.findRecord(expert);
				model.addAttribute("answerCount", expertList.size());
			}
			/*
			 * 抢答数量
			 */
			/*
			 * 审核专家数量
			 */
			List<IssueTypeEntity> issueTypeList = issueTypeService.findAll();
			
			model.addAttribute("issueTypeList", issueTypeList);
		}
		
		return "service/user";
	}
	
	@RequestMapping("updateStatus")
	public String updateStatus(UserEntity userEntity){
		System.out.println(userService.updateStatus(userEntity));
		return "redirect:findAllUI";
	}
	
	@RequestMapping("findUserIssue")
	public String findUserIssue(int issueTypeId,Model model,HttpServletRequest request){
		String openId = (String) request.getSession().getAttribute("openId"); 
		//用户信息
		UserEntity UserEntity = userService.findByOpenId(openId);
		model.addAttribute("userInfo", UserEntity);
		//问题类型
		List<IssueTypeEntity> issueTypeList = issueTypeService.findAll();
		model.addAttribute("issueTypeList", issueTypeList);
		//热搜
		List<TopSearchEntity> topSearchList = topSearchService.findTopSearch();
		model.addAttribute("topSearchList", topSearchList);
		
		//广告

		model.addAttribute("homeTop", advertisingService.findByLocation("home-top"));
		model.addAttribute("homeLine", advertisingService.findByLocation("home-line"));
		
		return "service/index";
	}
	
	@RequestMapping("findAllUserIssue")
	public void findAllUserIssue(int page,int size,int issueTypeId,HttpServletResponse response,HttpServletRequest request){
		
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("issueTypeId", issueTypeId);
		if(page!=0){
			 int cPage=page;
			 cPage=(cPage-1)*10;
			 map.put("page", cPage);
			 map.put("size", size);
		 }
		List<IssueEntity> list = issueService.findUserIssue(map);
		
		String json = JSON.toJSONString(list);
		try {
			response.getWriter().write(json);
			response.flushBuffer();
		} catch (Exception e) {
			System.out.println("JsonError："+e);
		}
		
	}
	
	@RequestMapping("findByExpertId")
	public void findByExpertId(int expertId,HttpServletResponse response){
		UserEntity userEntity = userService.findExpertId(expertId);
		
		String json = JSON.toJSONString(userEntity);
		try {
			response.getWriter().write(json);
			response.flushBuffer();
		} catch (Exception e) {
			System.out.println("JsonError："+e);
		}
	}
	
}
